package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1673 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] mostCompetitive(int[] nums, int k) {
            int remainToDelete = nums.length - k;
            Stack<Integer> st = new Stack<>();
            for (int num : nums) {
                while (!st.isEmpty() && remainToDelete > 0 && st.peek() > num) {
                    st.pop();
                    --remainToDelete;
                }
                if (st.size() < k) st.push(num);
                else --remainToDelete;
            }

            int[] res = new int[k];
            while (--k >= 0) res[k] = st.pop();
            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[71,18,52,29,55,73,24,42,66,8,80,2]");
        System.out.println(Arrays.toString(solution.mostCompetitive(arr, 3)));
    }

    public static void main(String[] args) throws Exception {
        LC1673 an = new LC1673();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
